import QtQuick 2.6
import QtQuick.Controls 1.5
import QtQuick.Layouts 1.1

import Cpp.Request 1.0 as Request

import UiBase 1.0
import Config 1.0

import "qrc:/Qml/Discover"
import "News" as News

ColumnLayout {
    id: root

    property var newsGroup: listModel.get(grid.currentIndex)
    property bool communicating: Request.NewsGroupManager.Communicating === newsGroupManager.status

    width: app.simulatorWidth
    height: app.simulatorHeight
    spacing: 0

    DiscoverHead {
        id: head
        Layout.fillWidth: true
        Layout.preferredHeight: 2 * 96.0 * Size.ratio
        z: 1
    }
    Rectangle {
        color: Color.foreground
        Layout.fillWidth: true
        Layout.fillHeight: true
        GridView {
            id: grid
            anchors.fill: parent
            cellWidth: 180 * Size.ratio
            cellHeight: 180 * Size.ratio
            delegate: SvgzImage {
                width: GridView.view.cellWidth
                height: GridView.view.cellHeight
                source: model.iconUrl
                LoaderMouseArea {
                    anchors.fill: parent
                    loader: Loader {
                        parent: indexRoot
                        Component {
                            id:newsComponent
                            News.Index {
                                groupId: newsGroup.id
                                groupName: newsGroup.name
                            }
                        }
                    }

                    onClicked: {
                        grid.currentIndex = index
                    }
                    onDoubleClicked: {
                        loader.sourceComponent = newsComponent
                    }
                }
            }
            model: ListModel {
                id: listModel
            }
        }
    }

    MouseArea {
        parent: editorRoot
        width: editGrid.width
        height: parent.height
        Rectangle {
            anchors.fill: parent
            color: Color.background
        }

        Grid {
            id:editGrid

            property alias newsGroup: root.newsGroup

            onNewsGroupChanged: {
                if(newsGroup)
                {
                    idLabel.text = String(newsGroup.id)
                    nameInput.text = newsGroup.name
                    iconUrlInput.text = newsGroup.iconUrl
                }
            }

            anchors.verticalCenter: parent.verticalCenter
            columns: 2

            Label {
                text: qsTr("id : ")
            }
            Label {
                id: idLabel
            }

            Label {
                text: qsTr("name : ")
            }
            TextField {
                id: nameInput
            }

            Label {
                text: qsTr("iconUrl : ")
            }
            TextField {
                id: iconUrlInput
            }

            CheckBox {
                id: addCheckbox
                text: qsTr("add")
            }
            Button {
                text: addCheckbox.checked ? qsTr("add") : qsTr("update")
                enabled: !communicating
                onClicked: {
                    var op
                    if (addCheckbox.checked)
                        op = newsGroupManager.add
                    else
                        op = newsGroupManager.update
                    op({
                           id: Number(idLabel.text),
                           name: nameInput.text,
                           iconUrl: iconUrlInput.text
                       })
                }
            }
            CheckBox {
                id:removeCheckbox
                text: qsTr("remove")
            }
            Button {
                text: qsTr("remove")
                enabled: removeCheckbox.checked && !communicating
                onClicked: {
                    newsGroupManager.remove(Number(idLabel.text))
                }
            }
        }
    }

    Request.NewsGroupQuery {
        id: newsGroupQuery
        onOk: {
            listModel.clear()
            for (var i in newsGroups) {
                listModel.append(newsGroups[i])
            }
        }
        onFailed : {
            app.setStatus(qsTr("newsGroupQuery failed : username or password error!"))
        }
        onError: {
            app.setStatus(qsTr("newsGroupQuery error : %1").arg(errorString))
        }

        Component.onCompleted: {
            queryAll()
        }
    }
    Request.NewsGroupManager {
        id: newsGroupManager
        onOk: {
            app.setStatus(qsTr("operation(add/update) successed."))
            newsGroupQuery.queryAll()
        }
    }
}
